package com.carl.mall.product.service.impl;

import com.carl.mall.product.dao.*;
import com.carl.mall.product.entity.*;
import com.carl.mall.product.service.*;
import com.carl.mall.product.vo.SkuItemSaleAttrVO;
import com.carl.mall.product.vo.SpuItemGroupAttrVO;
import com.carl.mall.product.vo.SpuItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carl.common.utils.PageUtils;
import com.carl.common.utils.Query;

import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {


    @Autowired
    private SkuInfoDao skuInfoDao;

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;


    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private AttrGroupService attrGroupService;


    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and((wrappers)->{
                wrappers.eq("sku_id",key)
                        .or().like("sku_name",key);
            });
        }
        // catalogId
        String catalogId = ( String)params.get("catelogId");
        if (!StringUtils.isEmpty(catalogId)&&!"0".equalsIgnoreCase(catalogId)){
            wrapper.eq("catalog_id",catalogId);
        }
        //brandId
        String brandId = ( String)params.get("brandId");
        if (!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(brandId)){
            wrapper.eq("brand_id",brandId);
        }

        //min
        String min = (String)params.get("min");
        if(!StringUtils.isEmpty(min)){
            wrapper.ge("price",min);
        }
        //max
        String max = (String)params.get("max");
        if (!StringUtils.isEmpty(max)){
            try {
                BigDecimal bigDecimal = new BigDecimal(max);
                if (bigDecimal.compareTo(new BigDecimal(0))>0){
                    wrapper.le("price",max);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> queryBySpuId(Long spuId) {
        List<SkuInfoEntity> skuInfoEntities = skuInfoDao
                .selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));

        return skuInfoEntities;
    }

    @Override
    public SpuItemVO item(Long skuId) throws ExecutionException, InterruptedException {
        SpuItemVO spuItemVO = new SpuItemVO();
        //1. sku的基本信息    pms_sku_info
        CompletableFuture<SkuInfoEntity> skuInfoEntityFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = this.getById(skuId);
            spuItemVO.setSkuInfoEntity(skuInfoEntity);
            return skuInfoEntity;
        }, threadPoolExecutor);
        //3.获取spu中的销售属性组
        CompletableFuture<Void> skuItemSaleAttrVOSFuture = skuInfoEntityFuture.thenAcceptAsync((res) -> {
            List<SkuItemSaleAttrVO> skuItemSaleAttrVOS = skuSaleAttrValueService.getSkuSaleAttrValueBySpuId(res.getSpuId());
            spuItemVO.setSkuItemSaleAttrVOS(skuItemSaleAttrVOS);
        }, threadPoolExecutor);
        //4.获取spu的介绍
        CompletableFuture<Void> spuDescFuture = skuInfoEntityFuture.thenAcceptAsync((res) -> {
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
            spuItemVO.setSpuInfoDescEntity(spuInfoDescEntity);
        }, threadPoolExecutor);
        //5.获取spu的规格参数
        CompletableFuture<Void> attrGroupFuture = skuInfoEntityFuture.thenAcceptAsync((res) -> {
            List<SpuItemGroupAttrVO> attrgroupWithSpuId = attrGroupService.getAttrgroupWithSpuId(res.getSpuId(), res.getCatalogId());
            spuItemVO.setItemGroupAttrVOS(attrgroupWithSpuId);
        }, threadPoolExecutor);
        //2. sku图片信息    pms_sku_images
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> skuImagesEntityList = skuImagesService.getImagesBySkuId(skuId);
            spuItemVO.setSkuImagesEntities(skuImagesEntityList);
        }, threadPoolExecutor);
        CompletableFuture.allOf(skuItemSaleAttrVOSFuture,spuDescFuture,attrGroupFuture,imageFuture).get();
        return spuItemVO;
    }

    @Override
    public List<String> querySkuSaleAttrsBySkuId(Long skuId) {
        return skuInfoDao.querySkuSaleAttrsBySkuId(skuId);
    }























}